package ${package_name}.service;

import ${package_name}.request.Add${model_name}Request;
import ${package_name}.request.Update${model_name}Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import org.springframework.util.StringUtils;

/**
* 描述：${table_annotation}服务层
* @author ${author}
* @date ${date}
* @配置URI:
* ${model_name?lower_case}:
     save${model_name}: http://base-service/baseserver/${model_name?lower_case}s
     update${model_name}: http://base-service/baseserver/${model_name?lower_case}s/{id}
     find${model_name}: http://base-service/baseserver/${model_name?lower_case}s/{id}
     delete${model_name}: http://base-service/baseserver/${model_name?lower_case}s/{id}
     find${model_name}s: http://base-service/baseserver/${model_name?lower_case}s?page={page}&size={size}&keyword={keyword}
*/
@Service
public class ${model_name}Service extends RestTemplateService{

    @Autowired
    private Environment environment;

    public ResponseEntity<?> save${model_name}(Add${model_name}Request addRequest) {
        String url = environment.getRequiredProperty("chdzGateway.endpoint.${model_name?lower_case}.save${model_name}");
        Map<String, Object> urlVariables = new HashMap<String, Object>();
        return this.add(url, urlVariables, addRequest);
    }

    public ResponseEntity<?> update${model_name}(String id, Update${model_name}Request updateRequest) {
        String url = environment.getRequiredProperty("chdzGateway.endpoint.${model_name?lower_case}.update${model_name}");
        Map<String, Object> urlVariables = new HashMap<String, Object>();
        urlVariables.put("id", id);
        return this.update(url, urlVariables, updateRequest);
    }

    public ResponseEntity<?> delete${model_name}(String id) {
        String url = environment.getRequiredProperty("chdzGateway.endpoint.${model_name?lower_case}.delete${model_name}");
        Map<String, Object> urlVariables = new HashMap<String, Object>();
        urlVariables.put("id", id);
        return this.delete(url, urlVariables);
    }

    public ResponseEntity<?> find${model_name}(String id) {
        String url = environment.getRequiredProperty("chdzGateway.endpoint.${model_name?lower_case}.find${model_name}");
        Map<String, Object> urlVariables = new HashMap<String, Object>();
        urlVariables.put("id", id);
        return this.findOne(url, urlVariables);
    }

    public ResponseEntity<?> find${model_name}s(Pageable pageable,HttpServletRequest request) {
        String url = environment.getRequiredProperty("chdzGateway.endpoint.${model_name?lower_case}.find${model_name}s");
        Map<String, Object> urlVariables = new HashMap<String, Object>();
        urlVariables.put("size", pageable.getPageSize());
        urlVariables.put("page", pageable.getPageNumber());
        
        StringBuffer sort = new StringBuffer();
		if (null != pageable.getSort()) {
			pageable.getSort().forEach(item -> {
				sort.append("&sort=").append(item.getProperty()).append(",").append(item.getDirection().name());
			});
		}

		url += StringUtils.isEmpty(sort) ? "" : sort;
		
        return this.findMultiple(url, urlVariables, request);
    }

}